package com.ciban.smarthome.module.utils;

import com.ciban.smarthome.common.utils.ConvertUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.wdl.utils.tools.EncAndDecUtils;

public class ModuleUtils {

    /**
     * 读取设备状态
     *
     * @param eNo
     * @param status
     * @return
     */
    public static int getEquipmentStatus(int eNo, String status) {
        int result = -1;
        if (!StringUtils.isEmpty(status)) {
            int pos = eNo % 8;
            int no = pos > 0 ? eNo / 8 + 1 : eNo / 8;
            int start = 2 * (no - 1);
            int end = 2 * no >= status.length() ? status.length() : 2 * no;
            String ss = status.substring(start, end);
            LogUtil.info("主机状态所在字节：" + ss);
            byte[] bs = ConvertUtils.stringToByte(ss);
            byte[] array = ConvertUtils.getByteArray(bs[0]);
            LogUtil.info("主机状态所在字节：" + array[0] + " " + array[1] + " "
                    + array[2] + " " + array[3] + " " + array[4] + " "
                    + array[5] + " " + array[6] + " " + array[7] + " ");
            pos = pos > 0 ? pos - 1 : 0;
            result = array[pos];
        }

        return result;
    }

    /**
     * 获取id的封装
     *
     * @return
     */
    public static String getIDByRule() {
        long l = System.currentTimeMillis();
        String idStr = String.valueOf(l);
        return EncAndDecUtils.MD5(idStr);
    }


}
